package com.wing.user.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.user.model.entity.ApplicationAuth;
import com.wing.user.model.request.ApplicationAuthForm;
import com.wing.user.service.ApplicationAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-管理", tags = "#/user/applicationAuth")
@RestController("applicationAuthController")
@RequestMapping("user/admin/applicationAuth")
@RequiredArgsConstructor
@Slf4j
public class ApplicationAuthController {

    private final ApplicationAuthService applicationAuthService;


    @ApiOperation(value = "查询所有", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ApplicationAuth> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
    ){

        Page<ApplicationAuth> pg = new Page<>(page, limit);
        QueryWrapper<ApplicationAuth> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }

        IPage<ApplicationAuth> list = applicationAuthService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody ApplicationAuthForm applicationAuthForm) {
        try{
            applicationAuthService.add(applicationAuthForm) ;
            return JsonResult.success("添加成功");
        }catch (Exception e) {
            log.error("添加失败", e);
            return JsonResult.fail("添加失败"+e.getMessage());
        }
    }

    @ApiOperation(value = "根据APPID查询对象", notes = "查询")
    @GetMapping("/getApplicationAuthByAppId")
    public JsonResult getApplicationAuthByAppId(@RequestParam("appId") String appId){
        QueryWrapper<ApplicationAuth> authQueryWrapper = new QueryWrapper<ApplicationAuth>();
        authQueryWrapper.eq("app_id", appId);
        ApplicationAuth applicationAuth = this.applicationAuthService.getBaseMapper().selectOne(authQueryWrapper);
        return JsonResult.success(applicationAuth);
    }



    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public JsonResult update(@RequestBody ApplicationAuthForm applicationAuthForm) {
        try {
            QueryWrapper<ApplicationAuth> authQueryWrapper = new QueryWrapper<ApplicationAuth>();
            authQueryWrapper.eq("app_id", applicationAuthForm.getAppId());
            ApplicationAuth applicationAuth = this.applicationAuthService.getBaseMapper().selectOne(authQueryWrapper);

            if (null == applicationAuth) {
                //插入
                applicationAuth = new ApplicationAuth();
                applicationAuth.setAppId(applicationAuthForm.getAppId());
                applicationAuth.setAuthInfo(applicationAuthForm.getAuthInfo());
                applicationAuthService.save(applicationAuth);
            } else {
                //修改
                applicationAuth.setAuthInfo(applicationAuthForm.getAuthInfo());
                applicationAuthService.updateById(applicationAuth);
            }

            return JsonResult.success("修改成功！");
        }catch (Exception e) {
            log.error("修改失败！",e);
            return JsonResult.fail("修改失败！"+e.getMessage());
        }
    }



    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applicationAuthId", value = "id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long applicationAuthId) {
        if (applicationAuthService.removeById(applicationAuthId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
